﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Configuration;

namespace Dados
{
    class VendaVOImpl:IVendaVO
    {


        public VendaVOImpl()
        {
            dataContext = new DigitalTVContextDataContext(ConfigurationManager.ConnectionStrings["DIGITALTVConnectionString"].ConnectionString);
        }

        public VendaVOImpl(string connectionString)
        {
            dataContext = new DigitalTVContextDataContext(connectionString);
        }

        public VendaVOImpl(DigitalTVContextDataContext dataContext)
        {
            this.dataContext = dataContext;
        }

        public Table<VendaVO> GetTable()
        {
            return dataContext.GetTable<VendaVO>();
        }

        #region CRUD

        public  void Inserir(VendaVO venda)
        {
            GetTable().InsertOnSubmit(venda);
        }

        public void Inserir(IEnumerable<VendaVO> lista)
        {
            GetTable().InsertAllOnSubmit(lista);
        }

        public void Atualizar(VendaVO  venda)
        {
            var table = GetTable();
            table.Attach(venda);
            dataContext.Refresh(RefreshMode.KeepCurrentValues, venda);
        }

        public void Atualizar(IEnumerable<VendaVO> lista)
        {
            GetTable().AttachAll(lista);
            dataContext.Refresh(RefreshMode.KeepCurrentValues, lista); 
        }

        public void Remover(VendaVO venda)
        {
            var table = GetTable();
            var apagar = table.Single(i => i.Equals(venda));
            table.DeleteOnSubmit(apagar);
        }

        public  void Remover(IEnumerable<VendaVO> lista)
        {
            var table = GetTable();
            var apagar = table.Where(i => lista.Contains(i));
            table.DeleteAllOnSubmit(apagar);
        }

        public  VendaVO  Obter(Func<VendaVO, bool> func)
        {
            return GetTable().Single(func);
        }

        public  IEnumerable<VendaVO> ObterTodos()
        {
            return GetTable();
        }

        public  IEnumerable<TRetorno> ObterTodos<TRetorno>(Func<VendaVO, TRetorno> selector)
        {
            return GetTable().Select(selector);
        }
        public  IEnumerable<VendaVO > ObterTodos(Func<VendaVO, bool> func)
        {
            return GetTable().Where(func);
        }

        public  IEnumerable<VendaVO> ObterTodos(int indice)
        {
            return GetTable().Skip(indice);
        }

        public  IEnumerable<VendaVO> ObterTodos(int indice, int quant)
        {
            return GetTable().Skip(indice).Take(quant);
        }

        public  IEnumerable<VendaVO> ObterTodos(int indice, Func<VendaVO, bool> func)
        {
            return GetTable().Where(func).Skip(indice);
        }

        public  IEnumerable<VendaVO> ObterTodos(int indice, int quant, Func<VendaVO, bool> func)
        {
            return GetTable().Where(func).Skip(indice).Take(quant);
        }

        public  List<VendaVO> ObterLista()
        {
            return GetTable().ToList();
        }

        public  List<TRetorno> ObterLista<TRetorno>(Func<VendaVO, TRetorno> func)
        {
            return GetTable().Select(func).ToList();
        }

        public  List<VendaVO> ObterLista(Func<VendaVO, bool> func)
        {
            return GetTable().Where(func).ToList();
        }
        public  List<VendaVO> ObterLista(int indice)
        {
            return GetTable().Skip(indice).ToList();
        }

        public  List<VendaVO> ObterLista(int indice, int quant)
        {
            return GetTable().Skip(indice).Take(quant).ToList();
        }

        public  List<VendaVO > ObterLista(int indice, Func<VendaVO, bool> func)
        {
            return GetTable().Where(func).Skip(indice).ToList();
        }

        public  List<VendaVO> ObterLista(int indice, int quant, Func<VendaVO, bool> func)
        {
            return GetTable().Where(func).Skip(indice).Take(quant).ToList();
        }

        #endregion

        protected  void IniciarTransacao()
        {
            dataContext.Connection.Open();
            dataContext.Transaction = dataContext.Connection.BeginTransaction();
        }

        protected virtual void ComitarTransacao()
        {
            try
            {
                dataContext.SubmitChanges();
                dataContext.Transaction.Commit();
            }
            catch(Exception ex)
            {
                dataContext.Transaction = null;
                if(dataContext.Connection.State == System.Data.ConnectionState.Open)
                    dataContext.Connection.Close();
                throw ex;
            }
        }

        protected  void SubmitChanges()
        {
            dataContext.SubmitChanges();
        }
    }

     
    }

